import  pygame

class Ship:

    def __init__(self, screen, ai_settings):
        # 增加的ai_settings用来获取其速度初始值
        self.screen = screen
        self.ai_settings = ai_settings

        self.image = pygame.image.load('images/孙豪式飞机.jpg')
        self.rect = self.image.get_rect()
        self.screen_rect = self.screen.get_rect()
        # 飞船的初始位置为屏幕的底部中央
        self.rect.centerx = self.screen_rect.centerx
        # 获得的是图片的中心位置[x,y]
        self.rect.bottom = self.screen_rect.bottom
        # 飞船的上下左右移动
        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False
        # 因为rect的等等属性只能存储整数类型，因此单独开个变量记录位置
        self.center_x = float(self.rect[0])
        self.center_y = float(self.rect[1])




    def update(self):
        """根据移动标志调整飞船的位置"""
        if self.moving_right and self.center_x < self.screen_rect.right-30:
            self.center_x += self.ai_settings.ship_speed_factor
        if self.moving_left and self.center_x > 0:
            self.center_x -= self.ai_settings.ship_speed_factor
        if self.moving_up and self.center_y > 0:
            self.center_y -= self.ai_settings.ship_speed_factor
        if self.moving_down and self.center_y < 800-40:
            self.center_y += self.ai_settings.ship_speed_factor


    def blitme(self):
        """在指定位置绘制飞船"""
        self.screen.blit(self.image, (self.center_x,self.center_y))
